package com.wumai.modernguilib.component;

import com.mojang.blaze3d.matrix.MatrixStack;
import com.mojang.blaze3d.platform.GlStateManager;
import net.minecraft.client.Minecraft;
import net.minecraft.client.gui.AbstractGui;
import net.minecraft.client.gui.FontRenderer;
import net.minecraft.util.text.ITextComponent;
import net.minecraft.util.text.StringTextComponent;

import static net.minecraft.util.math.MathHelper.floor;

public abstract class TextComponent extends BaseComponent {
    protected ITextComponent text;
    protected int textColor = 0xFFFFFFFF; // 默认白色文字
    protected int backgroundColor = 0x00000000; // 默认透明背景
    protected int borderColor = 0x00000000; // 默认无边框
    protected boolean hasBackground = false;
    protected boolean hasBorder = false;
    protected int textAlign = 1;
    protected float textScale = 1.0f;

    enum TextAlign {
        LEFT,
        CENTER,
        RIGHT
    }

    public void setTextScale(float textScale) {
        this.textScale = textScale;
    }

    public TextComponent(String text) {
        // 新的构造函数，不指定位置和大小，将由父容器或自动布局系统确定
        super(0, 0, 0, 0);
        this.text = new StringTextComponent(text);
    }

    public TextComponent(ITextComponent text) {
        super(0, 0, 0, 0);
        this.text = text;
    }

    public TextComponent(int x, int y, int width, int height, String text) {
        super(x, y, width, height);
        this.text = new StringTextComponent(text);
    }

    public TextComponent(int x, int y, int width, int height, ITextComponent text) {
        super(x, y, width, height);
        this.text = text;
    }

    // 抽象方法，子类需要实现具体的渲染逻辑
    @Override
    protected void renderContent(MatrixStack matrixStack, int mouseX, int mouseY, float partialTicks) {
        // 绘制文字
        FontRenderer fontRenderer = Minecraft.getInstance().font;
        String textString = text.getString();
        int textWidth = fontRenderer.width(textString);
        int textHeight = fontRenderer.lineHeight;

        // 考虑缩放后的实际尺寸
        int scaledTextWidth = (int) (textWidth * textScale);
        int scaledTextHeight = (int) (textHeight * textScale);

        int textX, textY;

        // 水平对齐 - 修正：使用缩放后的宽度进行计算
        switch (textAlign) {
            case 1: // 居中
                textX = x + (width - scaledTextWidth) / 2;
                break;
            case 2: // 右对齐
                textX = x + width - scaledTextWidth - 2;
                break;
            default: // 左对齐
                textX = x + 2;
        };

        // 垂直居中 - 修复计算方式，考虑文本缩放
        textY = y + (height - scaledTextHeight) / 2;

        GlStateManager._enableBlend();
        // 使用MatrixStack来进行变换，这样可以保证坐标不会发生偏移
        matrixStack.pushPose();
        // 考虑到缩放，我们需要调整Y坐标使文字在缩放后仍然垂直居中
        matrixStack.translate(textX, textY, 0);
        matrixStack.scale(textScale, textScale, 1.0f);
        matrixStack.translate(-textX, -textY, 0);
        fontRenderer.drawShadow(matrixStack, textString, textX, textY, textColor);
        matrixStack.popPose();
        GlStateManager._disableBlend();
    }

    @Override
    public void updateLayout() {

    }

    @Override
    protected void renderBackground(MatrixStack matrixStack, int mouseX, int mouseY, float partialTicks) {
        // 绘制背景
        if (hasBackground) {
            AbstractGui.fill(matrixStack, x, y, x + width, y + height, backgroundColor);
        }

        // 绘制边框
        if (hasBorder) {
            // 上边框
            AbstractGui.fill(matrixStack, x, y, x + width, y + 1, borderColor);
            // 下边框
            AbstractGui.fill(matrixStack, x, y + height - 1, x + width, y + height, borderColor);
            // 左边框
            AbstractGui.fill(matrixStack, x, y, x + 1, y + height, borderColor);
            // 右边框
            AbstractGui.fill(matrixStack, x + width - 1, y, x + width, y + height, borderColor);
        }

    }



    // 设置文本
    public void setText(String text) {
        this.text = new StringTextComponent(text);
    }

    public void setText(ITextComponent text) {
        this.text = text;
    }

    // 设置文字颜色
    public void setTextColor(int textColor) {
        this.textColor = textColor;
    }

    // 设置背景颜色
    public void setBackgroundColor(int backgroundColor) {
        this.backgroundColor = backgroundColor;
        this.hasBackground = (backgroundColor & 0xFF000000) != 0; // 如果有alpha通道则启用背景
    }

    // 设置边框颜色
    public void setBorderColor(int borderColor) {
        this.borderColor = borderColor;
        this.hasBorder = (borderColor & 0xFF000000) != 0; // 如果有alpha通道则启用边框
    }

    // 设置文本对齐方式
    public void setTextAlign(int textAlign) {
        this.textAlign = textAlign;
    }

    // 获取文本
    public ITextComponent getText() {
        return text;
    }

    public int getTextColor() {
        return textColor;
    }

    public int getBackgroundColor() {
        return backgroundColor;
    }

    public int getBorderColor() {
        return borderColor;
    }

    public void setPosition(int x, int y) {
        this.x = x;
        this.y = y;
    }

    // 添加一些方法来设置背景和边框属性
    public void setBackground(boolean hasBackground) {
        this.hasBackground = hasBackground;
    }

    public void setBorder(boolean hasBorder) {
        this.hasBorder = hasBorder;
    }

    public void setTextAlignCenter() {
        this.textAlign = 1;
    }
    
    public void setTextAlignRight() {
        this.textAlign = 2;
    }
    
    public void setTextAlignLeft() {
        this.textAlign = 0;
    }
}